﻿using System.Collections.Generic;
using UnityEngine;

public class UIManager : MonoSingleton<UIManager>
{
    [System.Serializable]
    public class UIItem
    {
        public string name;
        public GameObject uiObject;
    }
    private void Start()
    {
        OpenOtherUI("开始界面");
    }
    // 在Inspector中拖拽配置UI元素
    public List<UIItem> uiList = new List<UIItem>();

    // 通过名称打开UI
    public void OpenUI(string uiName)
    {
        foreach (var item in uiList)
        {
            if (item.name == uiName)
            {
                item.uiObject.SetActive(true);
                return;
            }
        }
        Debug.LogWarning($"找不到UI: {uiName}");
    }
    // 通过名称打开其他UI 关闭自身
    public void OpenOtherUI(string uiName)
    {
        foreach (var item in uiList)
        {
            if (item.name == uiName)
            {
                item.uiObject.SetActive(true);              
            }
            else
            {
                item.uiObject.SetActive(false);
            }
        }
    }
    // 切换UI状态
    public void ToggleUI(string uiName)
    {
        foreach (var item in uiList)
        {
            if (item.name == uiName)
            {
                item.uiObject.SetActive(!item.uiObject.activeSelf);
                return;
            }
        }
        Debug.LogWarning($"找不到UI: {uiName}");
    }   
    // 通过名称关闭UI
    public void CloseUI(string uiName)
    {
        foreach (var item in uiList)
        {
            if (item.name == uiName)
            {
                item.uiObject.SetActive(false);
                return;
            }
        }
        Debug.LogWarning($"找不到UI: {uiName}");
    }
    // 关闭所有UI
    public void CloseAllUI()
    {
        foreach (var item in uiList)
        {
            if (item.uiObject != null)
            {
                item.uiObject.SetActive(false);
            }
        }
    }
}